package com.judeDot;

import java.awt.Image;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class DotUtil {

	
	public static int[][] dotArray ;
	public static Image mapImage;
	
	
public static void readConfigFile(String filePath){
	
//	if(deviceView==null){
//		return;
//	}

//	dotArray = new int[deviceView.getWidth(null)][deviceView.getHeight(null)];
//	dotArray = new int[3024][90];
	
	File file = new File(filePath);
	
	try {
		FileReader fileReader = new FileReader(file);
		
		BufferedReader reader = new BufferedReader(fileReader);
		String lin = "";
		int i =0;
		try {
			while((lin = reader.readLine())!=null){
				byte[] bs = lin.getBytes();
				
		
				
				for(int j =0;j<bs.length;j++){
					dotArray[i][j] =  bs[j];
				}
				i++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}
	
	
}	
public static void main(String[] args) {
	DotUtil.readConfigFile("D:\\mapDot.txt");
	
	System.out.println(dotArray[21][88]);
	
}

public static boolean writeConfigFile(){
	
	if(mapImage==null){
		return false;
	}
	
	
	
	int map_width = mapImage.getWidth(null);
	int map_hight = mapImage.getHeight(null);
	if(dotArray==null){
	dotArray = new int[map_hight][map_width];
	}

	for(int map_i=0;map_i<map_hight;map_i++){
		for(int map_j=0;map_j<map_width;map_j++){
			dotArray[map_i][map_j] = 0;
		}
	}
	int size = PaintMapPanel.paintInfo.size();
	MyPoint p1,p2;
	for (int i = 0; i < size; i++) {
		if(i+1<size){
			p1 = (MyPoint) PaintMapPanel.paintInfo.elementAt(i);
			p2 = (MyPoint) PaintMapPanel.paintInfo.elementAt(i + 1);
			
				for(int i_y = p1.y;i_y<=p2.y;i_y++){
					for(int i_x = p1.x;i_x<=p2.x;i_x++){
					dotArray[i_y][i_x]=1;
				}
			}
			i = i + 1;
		}
	}
	
	
	
	StringBuilder sb = new StringBuilder();
	for(int map_i=0;map_i<map_hight;map_i++){
		for(int map_j=0;map_j<map_width;map_j++){
			sb.append("").append(dotArray[map_i][map_j]);
		}
		sb.append("\r\n");
	}
	
	

	try {
		File file = new File("d://test.text");
		FileWriter w = new FileWriter(file);
		
		BufferedWriter bf = new BufferedWriter(w);
		bf.write(sb.toString());
	} catch (IOException e) {
		e.printStackTrace();
		return false;
	}
	return true;
}	
	
}
